﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using NHibernate;
using Domain.Entities;
using Domain;

namespace NHibernateTest
{
    public partial class Form1 : Form
    {
        private Operations _operate;
        private ISession _session;
        private Helper _helper;
        public Form1()
        {
            InitializeComponent();
            _helper = new Helper();

            _session = _helper.GetSession();
            _operate = new Operations(_session);
            
        }

        /// <summary>
        /// 根据ID获取对象
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            Customer customer = _operate.GetCustomerById(Convert.ToInt32(this.tb_ID.Text));
            string firstname = customer.Firstname;
            this.tb_firstname.Text = firstname;
        }

        /// <summary>
        /// 对象保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_SaveCustomer_Click(object sender, EventArgs e)
        {
            Customer customer = new Customer();
            customer.Firstname = this.tb_firstname.Text;
            _operate.CreateCustomer(customer);
        }

        /// <summary>
        /// 获取所有Customer实例
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            List<Customer> list = new List<Customer>();
            list=_operate.From().ToList<Customer>();
            this.listBox1.DataSource = list;
            this.listBox1.DisplayMember = "Firstname";
        }

        /// <summary>
        /// 根据firstname获取Customer
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            List<Customer> list = new List<Customer>();
            list = _operate.GetCustomersByFirstName(this.tb_firstname.Text).ToList();
            this.listBox1.DataSource = list;
            this.listBox1.DisplayMember = "Id";
        }

        /// <summary>
        /// 获取Customer对象集合，设置最大5条
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            List<Customer> list = new List<Customer>();
            list = _operate.CreateCriteria().ToList();
            this.listBox1.DataSource = list;
            this.listBox1.DisplayMember = "Id";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            List<Customer> list = new List<Customer>();
            list = _operate.NarrowingAndOrder().ToList();
            this.listBox1.DataSource = list;
            this.listBox1.DisplayMember = "CustomerId";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Customer customer = new Customer();
            customer.Firstname = this.tb_firstname.Text;
            
            _operate.CreateCustomer(customer);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Customer customer = _operate.GetCustomerById(Convert.ToInt32(this.tb_ID.Text));
            //customer.Firstname = this.tb_firstname.Text;
            _operate.DeleteCustomer(customer);
        }

        /// <summary>
        /// HQL关联查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button8_Click(object sender, EventArgs e)
        {
            List<Customer> list = new List<Customer>();
            list = _operate.UseHQL_GetCustomersWithOrders(new DateTime(1901,01,01)).ToList();
            this.listBox1.DataSource = list;
            this.listBox1.DisplayMember = "CustomerId";
        }

        private void button9_Click(object sender, EventArgs e)
        {
            List<DTO> dto = new List<DTO>();
            dto=_operate.GetData().ToList();
            this.listBox1.DataSource = dto;
            this.listBox1.DisplayMember = "OrderDate";
        }
    }
}
